package dongze.com.ckdj.adapter;

import android.support.v7.util.AsyncListUtil;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import dongze.com.ckdj.R;

/**
 * Created by fanqwz on 2017/12/18.
 */

public class CheckInOperationImageItemMultiSelectionAdapter extends RecyclerView.Adapter<CheckInOperationImageItemMultiSelectionAdapter.ViewHolder> {
    private List<CheckInOperationItemData> data;
    private List<Integer> selectedPoss = new ArrayList<>();

    public CheckInOperationImageItemMultiSelectionAdapter(List<CheckInOperationItemData> data, String value) {
        this.data = data;
        String[] arr = value.split(",");
        for (int i = 0; i < arr.length; i++) {
            int index = data.indexOf(arr[i]);
            if (index != -1)
                selectedPoss.add(index);
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.check_in_operation_image_item, null);
        ViewHolder v = new ViewHolder(root);
        return v;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (data.size() > position && position >= 0) {
            CheckInOperationItemData d = data.get(position);
            holder.textView.setText(d.value);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectedPoss.contains(position)) {
                        selectedPoss.remove(position);
                    } else {
                        selectedPoss.add(position);
                    }
                    notifyItemChanged(position);
                }
            });
            holder.itemView.setSelected(selectedPoss.contains(position));
            Glide.with(holder.imageView.getContext())
                    .load(d.imgUrl)
                    .into(holder.imageView);
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
            textView = (TextView) itemView.findViewById(R.id.text);
        }
    }

    public String getValue() {
        Collections.sort(selectedPoss);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < selectedPoss.size(); i++) {
            sb.append(data.get(selectedPoss.get(i)).value);
            sb.append(",");
        }

        if (sb.lastIndexOf(",") == sb.length() - 1)
            sb.deleteCharAt(sb.length() - 1);

        return sb.toString();
    }
}
